<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" destroyOnClose showFooter title="转卡" :width="drawerWidth" @ok="handleSubmit">
    <div style="color:red;font-weight: bold">只允许转卡一次，如果选择申请函方式转卡，需要上传会员转卡申请函，要求身份证放在申请函上照片</div>
    <BasicForm @register="registerForm"></BasicForm>
  </BasicDrawer>
</template>
<script lang="ts" setup>
import { ref, unref } from 'vue';
import { BasicForm, useForm } from '@apps/antd-admin/src/components/Form/index';
import { BasicDrawer, useDrawerInner } from '@apps/antd-admin/src/components/drawer';
import { userGoodsChangeUserFormSchema } from '../data/data';
import { useAppInject } from '@apps/antd-admin/src/hooks/web/use-app-inject';
import { userGoodsChangeUserSub } from '../data/api';
import { useMessage } from '@apps/antd-admin/src/hooks/web/useMessage';

const { getIsMobile } = useAppInject();
const drawerWidth = ref('850px');
if (unref(getIsMobile)) {
  drawerWidth.value = '80%';
}
// 声明Emits
const emit = defineEmits(['success', 'register']);
const [registerForm, { resetFields, setFieldsValue, validate, saveOrUpdate, updateSchema }] = useForm({
  rowKey: 'userGoodsId',
  labelWidth: 150,
  labelCol: { span: 4 },
  schemas: userGoodsChangeUserFormSchema,
  showActionButtonGroup: false,
});

const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async data => {
  resetFields();
  setDrawerProps({ confirmLoading: false });
  let record = data.record;
  let newRecord = {
    userGoodsId: record.userGoodsId,
    mainAccount: record.mainAccount,
    newVipCardNo: record.vipCardNo,
    weChatId: record.weChatId,
  };
  console.info(newRecord);
  setFieldsValue({
    ...newRecord,
  });
});
const { createConfirm } = useMessage();

async function handleSubmit() {
  try {
    const values = await validate();
    createConfirm({
      iconType: 'info',
      title: '转卡只允许一次，请检查信息正确并谨慎操作',
      content: '是否确认转卡？',
      onOk: async () => {
        setDrawerProps({ confirmLoading: true });
        await userGoodsChangeUserSub(values)
          .then(res => {
            closeDrawer();
            emit('success', false);
            setDrawerProps({ confirmLoading: false });
          })
          .catch(error => {
            setDrawerProps({ confirmLoading: false });
          });
      },
    });
  } finally {
    setDrawerProps({ confirmLoading: false });
  }
}
</script>
